<template>
  <div class="app-container">
    <u-page title="商户管理">
      <div slot="header">
        <el-button type="primary" icon="el-icon-plus" @click="hdlOpen(newForm)">
          {{ textMap['create'] }}
        </el-button>
      </div>
      <u-filtered>
        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="商户名称" prop="merchantName">
            <el-input v-model="listQuery.merchantName" placeholder="请填写" clearable style="width: 170px" />
          </el-form-item>
          <el-form-item label="商户编码" prop="merchantNo">
            <el-input v-model="listQuery.merchantNo" placeholder="请填写" clearable style="width: 170px" />
          </el-form-item>
          <el-form-item label="商品编码" prop="goodsNo">
            <el-input v-model="listQuery.goodsNo" placeholder="请填写" clearable style="width: 170px" />
          </el-form-item>
          <el-form-item label="收银员" prop="cashierNo">
            <el-input v-model="listQuery.cashierNo" placeholder="请填写" clearable style="width: 170px" />
          </el-form-item>
          <el-form-item label="收款台" prop="deviceNo">
            <el-input v-model="listQuery.deviceNo" placeholder="请填写" clearable style="width: 170px" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <u-status v-model="listQuery.status" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button type="text" @click="hdlEdit(row)">编辑</el-button>
              <el-button type="text" @click="hdlDelete(row.id, row.name)">删除</el-button>
            </template>
          </el-table-column>
        </template>
      </u-table>
    </u-page>

    <u-dialog :title="textMap[dialogStatus]" :show.sync="dialogFormVisible" width="980px" @confirm="dialogStatus==='create'? hdlCreate():hdlUpdate()">
      <el-form ref="dialogForm" :model="dataForm" :rules="rules" label-width="100px">
        <el-form-item label="商户名称" prop="merchantName">
          <el-input v-model="dataForm.merchantName" placeholder="请填写商户名称" clearable />
        </el-form-item>
        <el-form-item label="商户编码" prop="merchantNo">
          <el-input v-model="dataForm.merchantNo" placeholder="请填写商户编码" clearable />
        </el-form-item>
        <el-form-item label="商品编码" prop="goodsNo">
          <el-input v-model="dataForm.goodsNo" placeholder="请填写商品编码" clearable />
        </el-form-item>
        <el-form-item label="收款员" prop="cashierNo">
          <el-input v-model="dataForm.cashierNo" placeholder="请填写收款员编码" clearable />
        </el-form-item>
        <el-form-item label="收银台" prop="deviceNo">
          <el-input v-model="dataForm.deviceNo" placeholder="请填写收银台编码" clearable />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dataForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </u-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      api: this.$u.api.PluginsSalesMerchant,
      dataForm: {},
      rules: {
        merchantName: [{ required: true, message: '商户名称必填', trigger: 'blur' }],
        merchantNo: [{ required: true, message: '商户编码必填', trigger: 'blur' }],
        goodsNo: [{ required: true, message: '商品编码必填', trigger: 'blur' }],
        cashierNo: [{ required: true, message: '收款员编码必填', trigger: 'blur' }],
        deviceNo: [{ required: true, message: '收银台编码必填', trigger: 'blur' }],
      },
      listQuery: {
        page: 1,
        pageSize: 10,
        totalCount: 1,
        code: '',
        status: ''
      },
      textMap: {
        update: '编辑商户',
        create: '新建商户'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      columns: [
        { prop: 'merchantName', label: '商户名称' },
        { prop: 'merchantNo', label: '商户编码' },
        { prop: 'goodsNo', label: '商品编码' },
        { prop: 'cashierNo', label: '收款员编码' },
        { prop: 'deviceNo', label: '收银台编码' },
        {
          prop: 'status',
          label: '状态',
          render: (h, params) => {
            return h('el-tag', {
              props: { type: params.row.status=== 1 ? 'success' : 'danger' }
            }, params.row.status ? '启用' : '禁用');
          }
        }
      ],
      listLoading: false,
      list: [],
      listOptions: {
        stripe: true
      },
      newForm: {
        status: 1
      }
    };
  },
  created() {
    this.hdlList();
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  .disabled {
    ::v-deep {
      .el-input {
        width: 140px;
      }
    }
  }
}
</style>
